﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Web.UI.WebControls;
using ARS.PublicTool.DataHandle;
using TextBox = System.Windows.Forms.TextBox;

namespace ARS.PublicTool.FormsHelper
{
    public class ModelControlMapping:BaseModel
    {
        public ModelControlMapping(object model, Control container, string nameStart = "")
        {
            this.Model = model;
            this.NameStart = nameStart;
            this.container = container;
        }

        public string NameStart
        {
            get;
            set;
        }

        public Control container
        {
            get;
            set;
        }

        public void ModelToControl()
        {
            foreach (PropertyInfo pi in typeModel.GetProperties())
            {
                object value = pi.GetValue(Model, null);
                if (value == null)
                {
                    continue;
                }
                string name = pi.Name; //获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                //获得属性的类型,进行判断然后进行以后的操作,例如判断获得的属性是整数
                //if (value.GetType() == typeof(int))
                //{
                //    //进行你想要的操作
                //    //Response.Write(name);
                //}

                Control[] ctls = container.Controls.Find(NameStart + name, false);
                if (ctls.Length > 0)
                {
                    Control control = ctls[0];
                    if (control is TextBox)
                    {
                        TextBox textBox = (TextBox)control;
                        textBox.Text = value.ToString();
                    }
                    if (control is RichTextBox)
                    {
                        RichTextBox rtb = (RichTextBox) control;
                        rtb.Text = value.ToString();
                    }
                }
                else
                {
                    
                }
                //System.Web.UI.Control control = container.FindControl(name);
                //if (control is TextBox)
                //{
                //    TextBox textBox = (TextBox)control;
                //    textBox.Text = value.ToString();
                //}
                //if (control is DropDownList)
                //{
                //    DropDownList drop = (DropDownList)control;
                //    drop.SelectedValue = value.ToString();
                //}

            }
        }

        public void ControlToModel()
        {
            foreach (Control control in container.Controls)
            {
                string value = null;
                if (control is TextBox)
                {
                    TextBox textBox = (TextBox) control;
                    value = textBox.Text;
                }
                else if (control is RichTextBox)
                {
                    RichTextBox rtb = (RichTextBox)control;
                    value = rtb.Text;
                }
                else
                {
                    continue;
                }
                if (string.IsNullOrEmpty(value))
                {
                    continue;
                }
                string fieldName; // = NameStart + control.Name;
                if (string.IsNullOrEmpty(NameStart))
                {
                    fieldName = control.Name;
                }
                else
                {
                    fieldName = control.Name.Replace(NameStart, "");
                }
                object objValue;
                Type fieldType = typeModel.GetProperty(fieldName).PropertyType;
                //Type t = typeof (Nullable<>);
                if (fieldType.Name == typeof (Nullable<>).Name)
                {
                    objValue = value.ConvertTo<Nullable<int>>();
                }
                else
                {
                    objValue = Convert.ChangeType(value, fieldType);
                }
                //object objValue = value.ConvertTo<Nullable<int>>();// Convert.ChangeType(value,fieldType )
                typeModel.GetProperty(fieldName).SetValue(Model, objValue, null);
            }
            //foreach (System.Web.UI.Control control in this.Controls)
            //{
            //    string value = null;
            //    if (control is TextBox)
            //    {
            //        TextBox textBox = (TextBox)control;
            //        value = textBox.Text;
            //    }
            //    else if (control is DropDownList)
            //    {
            //        DropDownList drop = (DropDownList)control;
            //        value = drop.SelectedValue;
            //    }
            //    else
            //    {
            //        continue;
            //    }

            //}
        }

    }
}
